package com.JWH_TV.utils

import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

class TokenUtils {

    // 修改为 public 方法
    companion object {
        fun generateToken(secretKey: String, timestamp: String): String {
            val rawStr = timestamp + secretKey
            return md5Hex(rawStr)
        }

        // MD5 计算方法
        private fun md5Hex(data: String): String {
            try {
                val md = MessageDigest.getInstance("MD5")
                val hash = md.digest(data.toByteArray())
                val hexString = StringBuilder()
                for (b in hash) {
                    val hex = Integer.toHexString(0xff and b.toInt())
                    if (hex.length == 1) hexString.append('0')
                    hexString.append(hex)
                }
                return hexString.toString()
            } catch (e: NoSuchAlgorithmException) {
                throw RuntimeException("MD5 algorithm not found", e)
            }
        }
    }
}